var _contextPath;
var sellerCode = "";
$(document).ready(function() {
	list();
	top.UppTabs.operator = top.getCurrentTagTitle();// 设置用户tab的标题值

		$("#sellers").autocomplete( {
			source : function(request, response) {
				$.ajax( {
					url : contextPath + '/iccard/report/getSellers',
					dataType : "json",
					data : {
						maxRows : 10,
						keyword : request.term
					},
					success : function(data) {
						sellerCode = "";
						response($.map(data, function(item) {
							return {
								label : item.value + ',' + item.label,
								value : item.label
							};
						}));
					}
				});
			},
			minLength : 2,
			select : function(event, ui) {
				sellerCode = ui.item.label;
			},
			open : function() {
				$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
			},
			close : function() {
				$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
			}
		});
	});

/**
 * 查询用户列表
 */
function list() {
	_contextPath = $("#contextPath").val();

	//var sellerCode = ""
	var s = "";
	var e = "";
	var merchantType = ""
	var merchantCheckStatus = "";

	$("#merchant_list")
			.datagrid(
					{
						onLoadError : function() {
							// uppAjaxError(data);
						},
						queryParams : {
							sellerCode : sellerCode,
							merchantCheckStatus : merchantCheckStatus,
							merchantType : merchantType,
							beginTime : s,
							endTime : e
						},
						columns : [ [
								{
									field : 'merchantId',
									title : '',
									hidden : true
								},
								{
									field : 'merchantName',
									title : '商家名称',
									width : 100,
									formatter : function(value, row) {
										return '<a style="cursor: pointer;color: blue;" onclick="javascipt:info(this,\''
												+ row.merchantId
												+ '\');" >'
												+ value + '</a>'
									}
								},
								{
									field : 'merchantEncode',
									title : '商家编码',
									width : 100
								},
								{
									field : 'createTime',
									title : '接入时间',
									width : 100,
									align : 'center',
									formatter : function(value, row, index) {
										return (new Date(Number(value)))
												.dateConvert();
									}
								},
								{
									field : 'corporation',
									title : '负责人',
									width : 100
								},
								{
									field : 'merchantType',
									title : '业务类型',
									width : 100,
									formatter : function(value, row, index) {
										if (value == '0') {
											return '餐厅';
										} else if (value == '1') {
											return '住宿';
										} else if (value == '2') {
											return '食品';
										} else if (value == '3') {
											return '服装';
										} else if (value == '4') {
											return '纪念品';
										} else if (value == '5') {
											return '酒吧';
										} else if (value == '6') {
											return '咖啡店';
										} else if (value == '7') {
											return '茶楼';
										} else {
											return '其他类';
										}
									}
								},
								{
									field : 'merchantCheckStatus',
									title : '审核状态',
									width : 100,
									align : 'center',
									formatter : function(value, row, index) {
										if (value == '0') {
											return '审核未通过';
										} else if (value == '1') {
											return '审核已通过';
										} else if (value == '2') {
											return '待审核';
										} else {
											return '';
										}
									}
								},
								{
									field : 'merchantStatus',
									title : '是否冻结',
									width : 100,
									align : 'center',
									formatter : function(value, row, index) {
										if (value == '0') {
											return '冻结';
										} else if (value == '1') {
											return '正常';
										} else {
											return '';
										}
									}
								},
								{
									field : 'telphone',
									title : '联系电话',
									width : 100
								},
								{
									field : 'action',
									title : '操作',
									width : 100,
									formatter : function(value, row) {
										// alert(row.begintime > new Date());
										var str = "";

										str += '<a style="cursor:pointer" onclick="javascipt:edit(this,\'' + row.merchantId + '\');" >修改</a>'

										if (row.is_delete) {
											str = str == "" ? str : str + " | ";
											str += '<a style="cursor:pointer" onclick="javascipt:remove(this,\'' + row.merchantId + '\');" >删除</a>'
										}
										if (row.merchantCheckStatus == '0'
												|| row.merchantCheckStatus == '2') {
											str = str == "" ? str : str + " | ";
											str += '<a style="cursor:pointer" onclick="javascipt:audit(this,\'' + row.merchantId + '\');" >审核</a>'
										}
										if (row.merchantStatus == '0') {
											str = str == "" ? str : str + " | ";
											str += '<a style="cursor:pointer" onclick="javascipt:enableMerchant(this,\'' + row.merchantId + '\');" >解冻</a>'
										} else if (row.merchantStatus == '1') {
											str = str == "" ? str : str + " | ";
											str += '<a style="cursor:pointer" onclick="javascipt:disableMerchant(this,\'' + row.merchantId + '\');" >冻结</a>'
										}
										return str;
									}
								} ] ],
						autoRowHeight : true,
						fitColumns : true,
						pagination : true,
						singleSelect : true,
						rownumbers : true
					});
}

function getContextPath() {
	if ("" == _contextPath) {
		_contextPath = $("#contextPath").val();
	}
	return _contextPath;
}

/**
 * 搜索
 * 
 * @return
 */
function searchKey() {
	var s = $('input[name="beginTime"]').val();
	var e = $('input[name="endTime"]').val();
	if (s == "" && e != "") {
		top.uppShow('请选择开始时间！');
		return;
	}
	if (s != "" && e == "") {
		top.uppShow('请选择结束时间！');
		return;
	}
	if (s > e) {
		top.uppShow('开始时间大于了结束时间，请重新选择!');
		return;
	}
	var merchantType = $("#merchantType").val();
	var merchantCheckStatus = $("#merchantCheckStatus").val();

	// var sellerCode = $("#sellers").val();
	// top.uppShow(s + " | " + e + " | " + merchantType + " | " +
	// merchantCheckStatus + " | " + sellerCode);
	$('#merchant_list').datagrid('load', {
		sellerCode : sellerCode,
		merchantCheckStatus : merchantCheckStatus,
		merchantType : merchantType,
		beginTime : s,
		endTime : e
	});
}

/**
 * 新增
 */
function add(obj) {
	var backHtml = $(obj).html();
	$(obj).html('商家管理-新增');
	$(obj).attr('hiddenhref', getContextPath() + "iccard/merchant/toAddView");
	window.parent.menuClick(obj);
	$(obj).html(backHtml);
}

/**
 * 编辑
 * 
 * @param obj
 * @param id
 * @return
 */
function edit(obj, id) {
	var backHtml = $(obj).html();
	$(obj).html('商家管理-编辑');
	$(obj).attr('hiddenhref',
			getContextPath() + "iccard/merchant/toEditView?merchantId=" + id);
	window.parent.menuClick(obj);
	$(obj).html(backHtml);
}

/**
 * 商家详情
 * 
 * @param obj
 * @param id
 * @return
 */
function info(obj, id) {
	var backHtml = $(obj).html();
	$(obj).html('商家管理-商家详情');
	$(obj).attr('hiddenhref',
			getContextPath() + "iccard/merchant/toInfoView?merchantId=" + id);
	window.parent.menuClick(obj);
	$(obj).html(backHtml);
}

/**
 * 审核
 * 
 * @param obj
 * @param id
 * @return
 */
function audit(obj, id) {
	var backHtml = $(obj).html();
	$(obj).html('商家管理-审核');
	$(obj).attr('hiddenhref',
			getContextPath() + "iccard/merchant/toAuditView?merchantId=" + id);
	window.parent.menuClick(obj);
	$(obj).html(backHtml);
}

/**
 * 解冻
 * 
 * @param obj
 * @param id
 * @return
 */
function enableMerchant(obj, id) {
	top.uppDialog.confirm('确定要解冻吗?', function() {
		$.ajax( {
			url : getContextPath()
					+ 'iccard/merchant/setStatus?merchantStatus=1&merchantId='
					+ id,
			type : 'POST',
			success : function(data) {
				top.uppShow(data.msgDesc);
				if ('0' == data.opResult) {
					window.location.href = getContextPath()
							+ "iccard/merchant/toListView";
				}
			}
		});
	});
}

/**
 * 冻结
 * 
 * @param obj
 * @param id
 * @return
 */

function disableMerchant(obj, id) {
	top.uppDialog.confirm('确定要冻结吗?', function() {
		$.ajax( {
			url : getContextPath()
					+ 'iccard/merchant/setStatus?merchantStatus=0&merchantId='
					+ id,
			type : 'POST',
			success : function(data) {
				top.uppShow(data.msgDesc);
				if ('0' == data.opResult) {
					window.location.href = getContextPath()
							+ "iccard/merchant/toListView";
				}
			}
		});
	});
}

window.top["reload_merchant_list"]=function(){
	$('#merchant_list').datagrid( "load");
};